package com.atguigu.java2;


import com.atguigu.java1.Person;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * 运行类的属性结构
 */
public class FieldTest {


    @Test
    public void test1() {
        Class<Person> clazz = Person.class;

        //获取当前运行时类以及父类声明为public访问权限的属性
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        System.out.println();
        //获取当前运行时类所有属性,无论什么权限修饰符,不包含父类
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
    }

    //权限修饰符,数据类型,变量名.....静态无法获取到
    @Test
    public void test2() {
        Class<Person> clazz = Person.class;
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            //1.权限修饰符
            int modifiers = declaredField.getModifiers();
//            System.out.println(Modifier.toString(modifiers));
            //2.数据类型
            Class<?> type = declaredField.getType();
//            System.out.println(type);

            //3.变量名

            String name = declaredField.getName();
            System.out.println(name);
        }
    }
}
